package com.sigmatrix.h5.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.dto.PackStandardData;
import com.sigmatrix.h5.dto.ProductData;
import com.sigmatrix.h5.dto.QRCodeData;
import com.sigmatrix.h5.dto.QRCodeForAddData;
import com.sigmatrix.h5.entity.CodeMapping;
import com.sigmatrix.h5.entity.FirstLevelQRCode;
import com.sigmatrix.h5.entity.MultistageLevelQRCode;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.exception.ServiceException;
import com.sigmatrix.h5.request.Request;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.CodeMappingService;
import com.sigmatrix.h5.service.FirstLevelQrCodeService;
import com.sigmatrix.h5.service.MultistageLevelQrCodeService;
import com.sigmatrix.h5.service.ProductService;
import com.sigmatrix.h5.service.QrCodeService;
import com.sigmatrix.h5.utils.Assert;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.utils.Utils;
import com.sigmatrix.h5.web.mode.QrCode;
import com.sigmatrix.h5.web.vo.QRCodeVo;

/**
 * 
 *@ClassName: QrCodeServiceImpl
 *@Description: 
 *@author MH
 *@date 2017年12月7日 下午12:28:30
 */
@Repository
public class QrCodeServiceImpl implements QrCodeService{
	
//	@Autowired
//	private FirstLevelQRCodeService firstLevelQRCodeService;
	
	@Autowired
	private MultistageLevelQrCodeService multistageLevelQRCodeService;
	@Autowired
	private CodeMappingService codeMappingService;
	
	
	/**logger record*/
	private static final Logger LOGGER = LoggerFactory.getLogger(QrCodeServiceImpl.class);
	
	private static final String MAP_PRODUCT = "product";
	
	/**map中存储的码级别*/
	private static final String MAP_CODE_LEVEL = "codeLevel";
	
	/**map中存储的一级码:firstLevelQRCode*/
	private static final String MAP_FIRST_LEVEL_QR_CODE = "firstLevelQRCode";
	
	/**map中存储的多级码:multistageLevelQRCode*/
	private static final String MAP_MULTISTAGE_LEVEL_QR_CODE = "multistageLevelQRCode";
	
	/**查询的二维码*/
	private static final String MAP_QUERY_CODE = "qRCode";
	
	/**一级码业务层接口*/
	@Autowired
	private transient FirstLevelQrCodeService firstLevelQRCodeService;
	
	/**产品业务层接口*/
	@Autowired
	private transient ProductService productService;
	
	/**支持的codeType*/
	public static List<Integer> supportCodeTypeList = new ArrayList<Integer>();
	
	static {
		supportCodeTypeList.add(Cons.CODE_TYPE_STANDARD);
		supportCodeTypeList.add(Cons.CODE_TYPE_FRIENDLY);
		supportCodeTypeList.add(Cons.CODE_TYPE_SEQNUM);
	}
	
	
	
	@Override
	public QrCode findByQrCode(String qrCode) {
		QrCode code = new QrCode();
		MultistageLevelQRCode mcode = multistageLevelQRCodeService.findByPackContent(qrCode);
		if(CommonUtils.isEmpty(mcode)){
			//FirstLevelQRCode fcode = firstLevelQRCodeService.findByQrCode(qrCode);
			CodeMapping mapping = codeMappingService.findByQrCode(qrCode);
			Assert.notNull(mapping, "码不存在,没有在mapping表");
			mcode = multistageLevelQRCodeService.findByPackContent(CommonUtils.encodeQRCode(mapping.getLogisticsCode()));
			Assert.notNull(mcode, "码不存在");
		}
		BeanUtils.copyProperties(mcode, code);
		code.setQrCode(qrCode);
		if(CommonUtils.isNotEmpty(mcode.getPackLevel()))code.setPackLevel(mcode.getPackLevel().shortValue());
		return code;
	}

	/**(non-Javadoc)
	 * @throws ServiceException 
	 *@Title: queryCodeForUpdateOutOrInStorage
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.QRCodeService#queryCodeForUpdateOutOrInStorage(com.sigmatrix.sc.handheld.inter.web.vo.QRCodeVo)
	 *@author youjun
	 *@date 2017年6月29日 下午2:54:57
	 */
	@Override
	public Response queryCodeForUpdateOutOrInStorage(final QRCodeVo qRCodeVo) throws ServiceException {

		try{
			Response validateResponse = validateParams(qRCodeVo, Cons.QR_CODE_OPERATION_STATUS_UPDATE);
			if(validateResponse.getCode().intValue()!=Cons.RESPONSE_CODE_SUCCESS) {
				return validateResponse;
			}
			if(qRCodeVo.getCodeType()==null){ // 确定是标准码还是友好码(输入码)或是序列号
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "码类型不能为空");
			}
			if(!supportCodeTypeList.contains(qRCodeVo.getCodeType())){
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "当前码类型系统不支持");
			}
			final int operationLevel = qRCodeVo.getOperationLevel();
			final Map<String, Object> map = new HashMap<String, Object>();
			validateResponse = queryCodeByCode(qRCodeVo, map, operationLevel);
			if(validateResponse.getCode().intValue()!=Cons.RESPONSE_CODE_SUCCESS) {
				return validateResponse;
			}
			// 查询操作方式(按垛则为垛码)码和该码对应的包装单位的数量
			return queryOperationLevelCode(map, operationLevel);
		} catch(Exception e) {
			throw new ServiceException(e);
		}
	}


	/**
	 *@Title: queryOperationLevelCode
	 *@Description: 查询操作方式(按垛则为垛码)码和该码对应的包装单位的数量等信息
	 *@return Response
	 *@author youjun
	 *@date 2017年7月4日 下午3:54:54
	 *@param map
	 *@param operationLevel
	 *@return
	 * @throws ServiceException 
	 */
	private Response queryOperationLevelCode(final Map<String, Object> map, final int operationLevel) throws ServiceException {

		if(CollectionUtils.isEmpty(map)
				||map.get(MAP_PRODUCT)==null) {
			throw new ServiceException("map不能为空");
		}
		final QRCodeData qRCodeData = new QRCodeData();
		final ProductData product = (ProductData) map.get(MAP_PRODUCT);
		final int packLevel = product.getProductUnit(); // 获得产品的包装单位级别,eg:2
		@SuppressWarnings("unused")
		final int codeLevel = (int) map.get(MAP_CODE_LEVEL); // 扫的码对应的包装级别
		@SuppressWarnings("unused")
		final String qRCode = (String) map.get(MAP_QUERY_CODE); // 扫的码
		qRCodeData.setProductId(product.getProductId());
		qRCodeData.setProductName(product.getProductName());
		
		if(operationLevel > packLevel) { // 操作方式(按垛)>包装单位(按箱),则找到垛码和垛码对应的箱码数量
			return queryHighLevelCode(map, operationLevel, packLevel);
		}else if(operationLevel==packLevel) { // 扫包装单位按包装单位出
			final MultistageLevelQRCode multistageLevelQRCode = (MultistageLevelQRCode) map.get(MAP_MULTISTAGE_LEVEL_QR_CODE);
			qRCodeData.setCode(multistageLevelQRCode.getPackContent());
			qRCodeData.setFriendlyCode(multistageLevelQRCode.getFriendlyCode());
			qRCodeData.setFriendlyCodeFront(multistageLevelQRCode.getFriendlyCodeFront());
			qRCodeData.setFriendlyCodeEnd(multistageLevelQRCode.getFriendlyCodeEnd());
			qRCodeData.setSeqNum(multistageLevelQRCode.getSeqNum());
			qRCodeData.setPackCodeArray(multistageLevelQRCode.getPackContent()); // 包装码的包装码集合就是他本身
			qRCodeData.setCount(1);
		}else if(operationLevel>1) { // 如:瓶盒箱层垛,按盒
			return queryHighLevelCode(map, operationLevel, 1);
		}else{ // 按单品单位
			final FirstLevelQRCode firstLevelQRCode = (FirstLevelQRCode) map.get(MAP_FIRST_LEVEL_QR_CODE);
			qRCodeData.setCode(firstLevelQRCode.getQrCode());
			qRCodeData.setFriendlyCode(firstLevelQRCode.getFriendlyCode());
			qRCodeData.setFriendlyCodeFront(firstLevelQRCode.getFriendlyCodeFront());
			qRCodeData.setFriendlyCodeEnd(firstLevelQRCode.getFriendlyCodeEnd());
			qRCodeData.setSeqNum(firstLevelQRCode.getSeqNum());
			qRCodeData.setCount(1);
		}
		return new Response(ResponseEnum.SUCCESS, qRCodeData);
	}


	/**
	 *@Title: queryHighLevelCode
	 *@Description: 查询高级别码
	 *@return Response
	 *@author youjun
	 *@date 2017年7月4日 下午4:48:35
	 *@param map
	 *@param operationLevel
	 *@param packLevel
	 *@return
	 */
	private Response queryHighLevelCode(final Map<String, Object> map, final int operationLevel, final int packLevel) throws ServiceException {

		if(CollectionUtils.isEmpty(map)
				||map.get(MAP_MULTISTAGE_LEVEL_QR_CODE)==null) {
			throw new ServiceException("参数有误");
		}
		final MultistageLevelQRCode multistageLevelQRCode = (MultistageLevelQRCode) map.get(MAP_MULTISTAGE_LEVEL_QR_CODE);
		final ProductData product = (ProductData)map.get(MAP_PRODUCT);
		final Map<Integer, String> packStandardMap = product.getPackStandardMap();
		// 获得操作方式级别码
		final MultistageLevelQRCode operationLevelMultistageLevelQRCode = getOperationLevelCode(multistageLevelQRCode, operationLevel);
		if(operationLevelMultistageLevelQRCode==null) {
			return new Response(Cons.RESPONSE_CODE_NO_DATA, new StringBuilder("当前")
					.append(packStandardMap.get(multistageLevelQRCode.getPackLevel()))
					.append("未关联").append(packStandardMap.get(operationLevel))
					.toString());
		}
		final QRCodeData qRCodeData = new QRCodeData();
		qRCodeData.setProductId(product.getProductId());
		qRCodeData.setProductName(product.getProductName());
		qRCodeData.setCode(operationLevelMultistageLevelQRCode.getPackContent());
		qRCodeData.setFriendlyCode(operationLevelMultistageLevelQRCode.getFriendlyCode());
		qRCodeData.setFriendlyCodeFront(operationLevelMultistageLevelQRCode.getFriendlyCodeFront());
		qRCodeData.setFriendlyCodeEnd(operationLevelMultistageLevelQRCode.getFriendlyCodeEnd());
		qRCodeData.setSeqNum(operationLevelMultistageLevelQRCode.getSeqNum());
		// 递归查找操作级别的码的对应包装单位级别码数量
		final Map<String, Object> codeRelatedInfoMap = multistageLevelQRCodeService.getInventedCodeRelatedInfo(multistageLevelQRCode.getEnterpriseCode(), operationLevelMultistageLevelQRCode.getPackContent(), 
				operationLevelMultistageLevelQRCode.getPackLevel(), false, packLevel, false);
		@SuppressWarnings("unchecked")
		final List<String> packCodeList = (List<String>) codeRelatedInfoMap.get("packCodeList");
		final int count = (int) codeRelatedInfoMap.get("packCodeCount");
//		final int count = recursionPackLevelCount(0, operationLevelMultistageLevelQRCode.getEnterpriseCode(), 
//				operationLevelMultistageLevelQRCode, product.getProductUnit().intValue());
		qRCodeData.setCount(count);
		final String packCodeArray = CollectionUtils.isEmpty(packCodeList)?"":packCodeList.toString().replace(" ", "").replace("[", "").replace("]", "");
		qRCodeData.setPackCodeArray(packCodeArray);
		return new Response(ResponseEnum.SUCCESS, qRCodeData);
	}

	/**
	 *@Title: recursionPackLevelCount
	 *@Description: 递归获得包装单位级别的数量
	 *@return int
	 *@author youjun
	 *@date 2017年7月4日 下午10:41:42
	 *@param sum
	 *@param enterpriseCode
	 *@param code
	 *@param productPackLevel
	 *@return
	 *@throws ServiceException
	 */
	@SuppressWarnings("unused")
	private int recursionPackLevelCount(final int sum, final String enterpriseCode, 
			final MultistageLevelQRCode code, final int productPackLevel) throws ServiceException {
		
		try{
			int totalPackNum = sum;
			if(code.getPackLevel()==productPackLevel+1) { // 当前码的包装级别比产品的包装单位大1说明下级码数量是包装单位级别的数量
				return code.getChildNum();
			}
			final String childQRCodes = code.getChilds();
			if(StringUtils.isNotBlank(childQRCodes)) {
				final String[] childQRCodeArray = childQRCodes.split(Cons.CHILD_CODE_SEPARATOR);
				MultistageLevelQRCode item = null;
				for(final String childCode:childQRCodeArray) {
					final Integer codeType = Cons.CODE_TYPE_STANDARD;
					item = multistageLevelQRCodeService.findMultistageLevelQRCodeByQRCode(enterpriseCode, childCode, codeType, false);
					totalPackNum=getPackLevelCount(totalPackNum, enterpriseCode, item, productPackLevel);
				}
			}
			
			return totalPackNum;
		}catch(Exception e){
			throw new ServiceException(e);
		}
		
	}
	/**
	 *@Title: getPackLevelCount
	 *@Description: 递归查找操作级别的码的对应包装单位级别码数量
	 *@return int
	 *@author youjun
	 *@date 2017年7月4日 下午6:53:34
	 *@param sum
	 *@param enterpriseCode
	 *@param code
	 *@param productPackLevel
	 *@return
	 *@throws ServiceException
	 */
	private int getPackLevelCount(final int sum, final String enterpriseCode, 
			final MultistageLevelQRCode code, final int productPackLevel) throws ServiceException {
		
		try{
			int totalPackNum = sum;
			if(code.getPackLevel()==productPackLevel+1) { // 当前码的包装级别比产品的包装单位大1说明下级码数量是包装单位级别的数量
				LOGGER.info("当前为包装单位级别{}的码,有{}个包装单位级别码", productPackLevel, code.getChildNum());
				totalPackNum+=code.getChildNum();
			}else{
				final String childQRCodes = code.getChilds();
				if(StringUtils.isNotBlank(childQRCodes)) {
					final String[] childQRCodeArray = childQRCodes.split(Cons.CHILD_CODE_SEPARATOR);
					MultistageLevelQRCode item = null;
					for(final String childCode:childQRCodeArray) {
						item = multistageLevelQRCodeService.findMultistageLevelQRCodeByQRCode(enterpriseCode, childCode, Cons.CODE_TYPE_STANDARD, false);
						return getPackLevelCount(totalPackNum, enterpriseCode, item, productPackLevel);
					}
				}
			}
			return totalPackNum;
		}catch(Exception e){
			throw new ServiceException(e);
		}
		
	}


	/**
	 *@Title: getOperationLevelCode
	 *@Description: 获得指定级别的码,<b>若无将返回null</b>
	 *@return MultistageLevelQRCode
	 *@author youjun
	 *@date 2017年7月4日 下午5:26:51
	 *@param multistageLevelQRCode
	 * @param operationLevel 
	 */
	private MultistageLevelQRCode getOperationLevelCode(final MultistageLevelQRCode multistageLevelQRCode, 
			final int operationLevel) throws ServiceException {
		
		try{
			LOGGER.info("根据码获得指定级别为：{}的码,一级一级往上查找,当前查找为:{}", operationLevel, JSONObject.toJSONString(multistageLevelQRCode));
			if(multistageLevelQRCode.getPackLevel()==operationLevel) {
				return multistageLevelQRCode;
			}
			MultistageLevelQRCode parentQRCode = null;
			if(StringUtils.isNotBlank(multistageLevelQRCode.getParentCode())) {
				parentQRCode = multistageLevelQRCodeService.
						findMultistageLevelQRCodeByQRCode(multistageLevelQRCode.getEnterpriseCode(), multistageLevelQRCode.getParentCode(),
								Cons.CODE_TYPE_STANDARD, false);
			}else{
				return null;
			}
			if(parentQRCode==null) {
				return null;
			}
			if(parentQRCode.getPackLevel()==operationLevel) {
				LOGGER.info("最终返回的指定级别为：{}的码为:{}", operationLevel, JSONObject.toJSONString(parentQRCode));
				return parentQRCode;
			}else{
				return getOperationLevelCode(parentQRCode, operationLevel);
			}
			
		}catch(Exception e){
			throw new ServiceException(e);
		}
	}


	/**
	 *@Title: queryCodeByCode
	 *@Description: TODO()
	 *@return Response
	 *@author youjun
	 *@date 2017年7月4日 下午2:46:26
	 *@param qRCodeVo
	 *@param map 验证通过后,map中将存储如下key:
	 *	<ul>
	 *		<li>1、<b>qRCode:</b>扫码的二维码</li>
	 *		<li>2、<b>firstLevelQRCode:</b>一级码</li>
	 *		<li>3、<b>multistageLevelQRCode:</b>多级码</li>
	 *		<li>4、<b>product:</b>产品信息</li>
	 *		<li>5、<b>codeLevel:</b>所扫码的级别</li>
	 *		<li>6、<b>parentCode:</b>上级码</li>
	 *	</ul>
	 *@param operationLevel 传入大于0的数字将会验证操作是否合法
	 *@return
	 *@throws ServiceException
	 */
	@Override
	public Response queryCodeByCode(final QRCodeVo qRCodeVo, Map<String, Object> map, final int operationLevel)
			throws ServiceException {
		
		try{
			if(qRCodeVo==null||map==null||StringUtils.isBlank(qRCodeVo.getCode())) {
				throw new ServiceException("参数有误");
			}
			final String enterpriseCode = qRCodeVo.getEnterpriseCode();
			String qRCode = qRCodeVo.getCode();
//			qRCode = qRCode.replaceAll("\r|\n", "");
			map.put(MAP_QUERY_CODE, qRCode);
			final int codeType = qRCodeVo.getCodeType()!=null?qRCodeVo.getCodeType():Cons.CODE_TYPE_STANDARD;
			final boolean isNeedConversion = codeType==Cons.CODE_TYPE_STANDARD?true:false;
			final FirstLevelQRCode firstLevelQRCode = firstLevelQRCodeService.findFirstLevelQRCodeByCodeAndCodeType(enterpriseCode, qRCode, isNeedConversion, codeType);
			Integer productId = null; // 产品ID
			Integer codeLevel = null; // 当前码的级别
			Short packLevel = null; // 产品的包装单位级别
			String parentCode = ""; // 上级码
			if(firstLevelQRCode==null) {
				final MultistageLevelQRCode multistageLevelQRCode = multistageLevelQRCodeService.
						findMultistageLevelQRCodeByQRCode(enterpriseCode, qRCode, codeType, isNeedConversion);
				if(multistageLevelQRCode==null) {
					return new Response(Cons.RESPONSE_CODE_NO_DATA, "码数据不存在");
				}
				parentCode = multistageLevelQRCode.getParentCode();
				map.put(MAP_MULTISTAGE_LEVEL_QR_CODE, multistageLevelQRCode);
				productId = multistageLevelQRCode.getProductId();
				codeLevel = multistageLevelQRCode.getPackLevel();
				LOGGER.info("查询到的多级码为:{}", JSONObject.toJSONString(multistageLevelQRCode));
			}else{
				productId = firstLevelQRCode.getProductId();
				codeLevel = 1;
				parentCode = firstLevelQRCode.getParentCode();
				LOGGER.info("查询到的单品码为:{}", JSONObject.toJSONString(firstLevelQRCode));
			}
			map.put(MAP_FIRST_LEVEL_QR_CODE, firstLevelQRCode);
			if(productId==null) {
				return new Response(Cons.RESPONSE_CODE_DATA_EXCEPTION, "码中未关联产品ID");
			}
			// 根据产品ID查找产品
			final ProductData product = productService.getProductFromCache(productId, enterpriseCode);
			if(product==null) {
				return new Response(Cons.RESPONSE_CODE_NO_DATA, "产品不存在");
			}
			packLevel = product.getProductUnit(); // 包装级别
			if(packLevel==null) {
				return new Response(Cons.RESPONSE_CODE_DATA_EXCEPTION, "码对应的产品"+product.getProductName()+"未设置包装单位");
			}
			if(codeLevel==null) {
				return new Response(Cons.RESPONSE_CODE_DATA_EXCEPTION, "码没有级别信息");
			}
			map.put(MAP_CODE_LEVEL, codeLevel);
			map.put(MAP_PRODUCT, product);
			map.put("parentCode", parentCode);
			final List<PackStandardData> packList = product.getPackStandard();
			if(CollectionUtils.isEmpty(packList)) {
				return new Response(Cons.RESPONSE_CODE_DATA_EXCEPTION, "产品未设置包装规格");
			}
			/*String operationLevelName = "";
			for(final PackStandardData item:packList) {
				if(item.getLevel().intValue()==codeLevel.intValue()) {
					operationLevelName = item.getName();
					break;
				}
			}*/
			if(operationLevel>0) {
				/*// 当前码级别(eg：箱码<2>)必须<=操作级别(eg:按垛<3>)
				if(operationLevel<codeLevel) {
					return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "操作方式不能小于当前码级别:"+operationLevelName);
				}*/
				final Map<Integer, String> packStandardMap = product.getPackStandardMap();
				if(operationLevel>=packLevel) { // 操作级别>=包装级别时,码级别必须大于等于包装级别
					if(codeLevel < packLevel) {
						final StringBuilder errorMsg = new StringBuilder("扫");
						errorMsg.append(packStandardMap.get(codeLevel)).append("码,不能按");
						errorMsg.append(packStandardMap.get(operationLevel)).append("出");
						LOGGER.error(errorMsg.toString());
						return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, errorMsg.toString());
					}
				}else{ // 操作级别小于包装级别
					if(codeLevel != operationLevel){ // 扫码级别不能等于操作级别
						final StringBuilder errorMsg = new StringBuilder("扫");
						errorMsg.append(packStandardMap.get(codeLevel)).append("码,不能按");
						errorMsg.append(packStandardMap.get(operationLevel)).append("出");
						LOGGER.error(errorMsg.toString());
						return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, errorMsg.toString());
					}
				}
			}
			return new Response(ResponseEnum.SUCCESS);
			
		}catch(Exception e){
			throw new ServiceException(e);
		}
	}


	/**(non-Javadoc)
	 *@Title: queryCodeForAddOutOrInStorage
	 *@Description: 
	 *@see com.sigmatrix.sc.handheld.inter.service.QRCodeService#queryCodeForAddOutOrInStorage(com.sigmatrix.sc.handheld.inter.web.vo.QRCodeVo)
	 *@author youjun
	 *@date 2017年7月3日 下午9:14:38
	 */
	@Override
	public Response queryCodeForAddOutOrInStorage(final QRCodeVo qRCodeVo) throws ServiceException {

		try{
			Response validateResponse = validateParams(qRCodeVo, Cons.QR_CODE_OPERATION_STATUS_NEW);
			if(validateResponse.getCode()!=Cons.RESPONSE_CODE_SUCCESS) {
				return validateResponse;
			}
			final QRCodeForAddData qRCodeForAddData = new QRCodeForAddData();
			final Map<String, Object> map = new HashMap<String, Object>();
			validateResponse = queryCodeByCode(qRCodeVo, map, -1);
			if(validateResponse.getCode().intValue()!=Cons.RESPONSE_CODE_SUCCESS) {
				return validateResponse;
			}
			final ProductData productData = (ProductData) map.get(MAP_PRODUCT);
			Integer codeLevel = (Integer) map.get(MAP_CODE_LEVEL);
			BeanUtils.copyProperties(productData, qRCodeForAddData);
			qRCodeForAddData.setCodeLevel(codeLevel);
			return new Response(ResponseEnum.SUCCESS, qRCodeForAddData);
		}catch(Exception e) {
			throw new ServiceException(e);
		}
	}
	
	/**
	 *@Title: validateParams
	 *@Description: 参数验证
	 *@return Response
	 *@author youjun
	 *@date 2017年6月29日 下午2:55:50
	 *@param qRCodeVo
	 *@return
	 */
	private Response validateParams(final QRCodeVo qRCodeVo, final int operationStatus) {
		final Response validateResponse = validateBasicParams(qRCodeVo);
		if(validateResponse.getCode().intValue()!=Cons.RESPONSE_CODE_SUCCESS) {
			return validateResponse;
		}
		if(StringUtils.isBlank(qRCodeVo.getCode())) {
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "查询的码不能为空");
		}
		if(qRCodeVo.getOperationType()==null) {
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "请明确当前的操作类型(出库或入库)");
		}
		// 当出、入库单存在的时候,操作的包装级别必须传递
		if(operationStatus==Cons.QR_CODE_OPERATION_STATUS_UPDATE) {
			if(qRCodeVo.getWarehouseId()==null) {
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "仓库ID不能为空");
			}
			if(qRCodeVo.getOperationLevel()==null) {
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "操作的包装级别不能为空");
			}
		}
		return new Response(ResponseEnum.SUCCESS);
	}
	
	/**
	 *@Title: validateBasicParams
	 *@Description: 验证公共参数,<b>企业code、用户ID不能为空,非该逻辑请调用其他</b>
	 *@return Response
	 *@author youjun
	 *@date 2017年6月27日 上午11:10:14
	 *@param request
	 */
	protected Response validateBasicParams(final Request request) {
		if(Utils.isEmpty(request)) {
			return new Response(ResponseEnum.PARAM_NULL);
		}
		if(Utils.isEmpty(request.getEnterpriseCode())){
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "企业code不能为空");
		}
		if(Utils.isEmpty(request.getUserId())){
			return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "用户ID不能为空");
		}
		return new Response(ResponseEnum.SUCCESS);
	}
	
	public static void main(String[] args) {
		System.out.println((Integer)129==(Integer)129);
	}
}
